<?php

namespace app\api\controller;

use app\common\controller\Api;

/**
 * 下载中心
 *
 * @icon fa fa-circle-o
 */
class Download extends Api
{

    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    /**
     * News模型对象
     * @var \app\common\model\Download
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\Download;
    }

    /**
     * 所有数据
     *
     * @ApiMethod   (GET)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="category_id", type="int", required=false, description="分类id")
     * @ApiParams   (name="pageNumber", type="int", required=false, description="当前页")
     * @ApiParams   (name="pageSize", type="int", required=false, description="分页大小")
     */
    public function pageList()
    {
        //当前页
        $page = $this->request->request("pageNumber");
        //分页大小
        $pagesize = $this->request->request("pageSize");
        $category_id = $this->request->request("category_id");
        $where = ['status'=>'normal'];
        if($category_id){
            $where['category_id'] = $category_id;
        }
        $list = $this->model->where($where)->page($page,$pagesize)->select();
        $total = $this->model->where($where)->count();

        $this->success('success', ['list' => $list, 'total' => $total]);
    }

    /**
     * 新增
     *
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="title", type="string", required=true, description="标题")
     * @ApiParams   (name="category_id", type="int", required=true, description="分类id")
     * @ApiParams   (name="url", type="string", required=true, description="内容链接")
     * @ApiParams   (name="id", type="int", required=true, description="主键id，传递是编辑，不传则新增")
     *
     */
    public function add()
    {
        $param = $this->request->post(false);
        $param['status'] = 'normal';
        $ret = $this->model->save($param);
        if ($ret) {
            $this->success('success', $this->model->getLastInsID());
        } else {
            $this->error($this->model->getError());
        }
    }

    /**
     * 详情
     *
     * @ApiMethod   (GET)
     * @ApiParams   (name="id", type="int", required=true, description="主键id")
     *
     */
    public function detail()
    {
        $id = $this->request->param('id');
        $row = $this->model->get(['id' => $id]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $this->success('success', $row);
    }

    /**
     * 编辑
     *
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="title", type="string", required=true, description="标题")
     * @ApiParams   (name="category_id", type="int", required=true, description="分类id")
     * @ApiParams   (name="url", type="string", required=true, description="内容链接")
     * @ApiParams   (name="id", type="int", required=true, description="主键id")
     *
     */
    public function edit()
    {
        $id = $this->request->param('id');
        $row = $this->model->get(['id' => $id]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $param = $this->request->post(false);
        $ret = $this->model->where(['id'=>$id])->update($param);
        if ($ret) {
            $this->success('success');
        } else {
            $this->error($this->model->getError());
        }
    }

    /**
     * 删除
     *
     * @ApiMethod   (DELETE)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="int", required=true, description="id")
     *
     */
    public function del()
    {
        $id = $this->request->param('id',0);
        if (!$id) {
            $this->error(__('id错误'));
        }
        $one = $this->model->find(['id'=>$id]);
        if (!$one) {
            $this->error(__('数据不存在'));
        }
        $this->success('success', $this->model->where('id',$id)->delete());
    }
}
